package Test3;

import java.util.*;

public class Main2 {
    public static void main(String[] args) {
        List<String> List1 = new ArrayList<>();
        List<String> List2 = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        String person = sc.next();
        String a[] = person.split("-");
        int n = Integer.parseInt(a[0]);
        for (int i = 0; i < n; i++) {
            String studen = sc.next();
            String b[] = studen.split("-");
            if (b[0].equals(a[1])) {
                List1.add(studen);
                if (b[1].equalsIgnoreCase(a[2])) {
                    List2.add(studen);
                }
            }
        }
        System.out.println(List1);
        System.out.println(List2);
        int[] grade = new int[List2.size()];
        for (int j = 0; j < List2.size(); j++) {
            String c[] = ((String) List2.get(j)).split("-");
            grade[j] = Integer.parseInt(c[2]);
        }

        for (int i = 0; i < List2.size() - 1; i++) {
            for (int j = i + 1; j < List2.size(); j++) {
                if (grade[i] < grade[j]) {
                    Collections.swap(List2, i, j);
                    int t = grade[i];
                    grade[i] = grade[j];
                    grade[j] = t;
                }
            }
        }
        System.out.println(List2);
    }
}
